﻿using B.S.BasicData.Api.Read.Application.Command.BaoBiao;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domian.Entities.Account;
using B.S.BasicData.Domian.Entities.shenpi;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System.Xml.Linq;

namespace B.S.BasicData.Api.Read.Application.Handler.BaoBiao
{
    public class GetmingHandler : IRequestHandler<GetmingCommand, ApiResult<List<BaoMingDTO>>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;
        private readonly IBaseRepository<LeaveModel> leaveRepo;

        public GetmingHandler(IBaseRepository<AccountModel> accountRepo, IBaseRepository<LeaveModel> leaveRepo)
        {
            this.accountRepo = accountRepo;
            this.leaveRepo = leaveRepo;
        }
        public Task<ApiResult<List<BaoMingDTO>>> Handle(GetmingCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<List<BaoMingDTO>>();
            var account = accountRepo.GetValue(request.Uid);
            if (account == null)
            {
                res.Message = "用户不存在";
                res.Code = ApiEnum.Error;
                return Task.FromResult(res);
            }
            var leave = leaveRepo.GetAll().Where(x => x.Uid == request.Uid && x.BeginTime.Month == request.Mouth).Select(x=>new BaoMingDTO
            {               
                Uid          =x.Uid         ,
                Name         =x.Name        ,
                Mouth        =request.Mouth ,
                BeginTime    =x.BeginTime   ,
                OverTime     =x.OverTime    ,
                CountTime    =x.CountTime   ,
                ShenpiReason =x.ShenpiReason,
            });
            res.Message = "查询明细成功";
            res.Code =ApiEnum.Ok;
            res.Data= leave.ToList();
            return Task.FromResult(res);
        }
    }
}
